<template>
  <el-container class="main_con">
    <div class="filter_con nopt">
      <div class="filter_area">
        <!-- <el-input class="serach_input mr_15" size="medium" placeholder="商品名称" style="width:300px;"></el-input> -->
        <el-input size="medium" v-model="obj.keyword" placeholder="请输入搜索内容" class="mr_15 mt_15" style="width:400px;">
          <el-select v-model="obj.type" slot="prepend" placeholder="请选择" style="width:130px;">
            <el-option v-for="item in type" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-input>

        <label class="mr_15 mt_15">支付方式</label>
        <el-select v-model="obj.pay_type" size="medium" placeholder="请选择" class="mr_15 mt_15">
          <el-option v-for="item in pay_type" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>

        <label class="mr_15 mt_15">维权类型</label>
        <el-select v-model="obj.after_type" size="medium" placeholder="请选择" class="mr_15 mt_15">
          <el-option v-for="item in after_type" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>

        <label class="mr_15 mt_15">状态</label>
        <el-select v-model="obj.status" size="medium" placeholder="请选择" class="mr_15 mt_15">
          <el-option v-for="item in status" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>

        <label class="mr_15 mt_15">维权状态</label>
        <el-select v-model="obj.after_status" size="medium" placeholder="请选择" class="mr_15 mt_15">
          <el-option v-for="item in after_status" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>

        <br />

        <label class="mr_15 mt_15">创建时间</label>
        <date-picker size="medium" @change="getTime" class="mr_15 mt_15"></date-picker>
        <el-button class="mr_15 mt_15" @click="searchList" size="medium">搜索</el-button>
      </div>
    </div>
    <div class="list_con">
      <div class="orderlist">
        <div class="order_head">
          <table cellspacing="0" cellpadding="0" width="100%">
            <colgroup>
              <col />
              <col width="150" />
              <col width="180" />
              <col width="180" />
              <col width="180" />
              <col width="150" />
              <col width="130" />
              <col width="150" />
            </colgroup>
            <tbody>
              <tr>
                <td>商品</td>
                <td>单价/数量</td>
                <td>维权状态</td>
                <td>买家</td>
                <td>实付款</td>
                <td>支付/配送</td>
                <td>状态</td>
                <td>操作</td>
              </tr>
            </tbody>
          </table>
        </div>

        <div class="order" v-for="(item, i) in list" :key="i">
          <div class="header">
            <div class="txt">
              <div class="info">下单时间：{{ item.create_time }}</div>
              <div class="info">订单编号：{{ item.order_no }}</div>
              <div class="info"><el-button type="text" @click="doCopy(item.order_no)">复制</el-button></div>
            </div>
            <div class="button">
              <!--  <el-button type="text">打印小票</el-button>
              <el-button type="text">发货单</el-button>
              <el-button type="text">电子面单</el-button> -->
            </div>
          </div>
          <div class="content">
            <table cellspacing="0" cellpadding="1" border="1" bordercolor="#dcdfe6" style="border-collapse:collapse;" width="100%">
              <colgroup>
                <col />
                <col width="150" />
                <col width="180" />
                <col width="180" />
                <col width="180" />
                <col width="150" />
                <col width="130" />
                <col width="150" />
              </colgroup>
              <tbody>
                <tr v-for="(sitem, l) in item.orderCommoditys" :key="l">
                  <td>
                    <div class="pro">
                      <div class="img"><vimage style="width: 80px; height: 80px;" :src="sitem.master" fit="fit"></vimage></div>
                      <div class="txt">
                        <div class="name">
                          <span :class="'t' + sitem.type">{{ sitem.type == 0 ? '实' : '虚' }}</span>
                          {{ sitem.name }}
                        </div>
                        <div class="class">{{ sitem.pvs_value_new }}</div>
                        <el-popover placement="top-start" title="附加规格
                        " width="400" trigger="hover" >
                          <el-table :data="sitem.attachlist" >
                            <el-table-column  property="attach_value" label="附加"></el-table-column>
                            <el-table-column  property="num" label="数量"></el-table-column>
                            <el-table-column  property="attach_price" label="单价"></el-table-column>
                          </el-table>
                          <!--  <el-button >hover 激活</el-button> -->
                          <div slot="reference" v-if="sitem.attachlist && sitem.attachlist.length > 0">附加x{{ sitem.attachlist.length }},共￥{{ sitem.attach_pirce }}</div>
                        </el-popover>
                      </div>
                    </div>
                  </td>
                  <td>
                    ￥{{ sitem.sell_price }}
                    <br />
                    X{{ sitem.count }}
                  </td>

                  <td v-if="item.after_type == 2 || item.orderCommoditys.length == 1 || l == 0" :rowspan="item.after_type == 1 ? item.orderCommoditys.length : 1">
                    <span>{{ sitem.after_type | after_status_text }}</span>
                    <span v-if="sitem.orderAfter">,{{ sitem.orderAfter.state_text }}</span>
                    <br />
                    <!-- <el-link v-if="sitem.after_type" :underline="false" type="primary" @click.stop="showAfter(sitem.order_after_id)">维权详情</el-link>  -->
                    <el-link v-if="sitem.order_after_id" :underline="false" type="primary" @click.stop="showAfter(sitem.order_after_id)">维权详情</el-link>
                  </td>

                  <td v-if="item.orderCommoditys.length == 1 || l == 0" :rowspan="item.orderCommoditys.length">
                    <el-tooltip :content="'收货地址：' + item.address" placement="bottom-start">
                      <span style="cursor: pointer">
                        {{ item.user_real_name }}
                        <br />
                        {{ item.user_mobile }}
                      </span>
                    </el-tooltip>
                  </td>
                  <td v-if="item.orderCommoditys.length == 1 || l == 0" :rowspan="item.orderCommoditys.length">
                   <!-- ￥{{ item.money }}
                    <br />
                    (含运费：￥{{ item.freight }}) -->
                    <el-popover placement="top" title="" width="170" trigger="hover">
                      <div class="pricecount">
                        <div>
                          <span>商品小计:</span>
                          <span>{{ item.orderItemCountPrice }}</span>
                        </div>
                        <div>
                          <span>运费:</span>
                          <span>￥{{ item.freight || '0.00' }}</span>
                        </div>
                        <div>
                          <span>优惠抵扣:</span>
                          <span>￥{{ item.discount || '0.00' }}</span>
                        </div>
                        <div>
                          <span>积分抵扣:</span>
                          <span>￥{{ item.discount_amount || '0.00' }}</span>
                        </div>

                        <div>
                          <span>会员折扣:</span>
                          <span>￥{{ item.discount_level_money }}</span>
                        </div>
                        <div>
                          <span>实付款:</span>
                          <span style=" color: #f56c6c;">￥{{ item.money }}</span>
                        </div>
                      </div>

                      <span slot="reference">
                        ￥{{ item.money }}
                        <br />
                        (含运费：￥{{ item.freight || '0.00' }})
                      </span>
                    </el-popover>
                  </td>
                  <td v-if="item.orderCommoditys.length == 1 || l == 0" :rowspan="item.orderCommoditys.length">
                    后台确认
                    <br />
                    快递
                  </td>
                  <td v-if="item.orderCommoditys.length == 1 || l == 0" :rowspan="item.orderCommoditys.length">{{ item.status }}</td>
                  <td v-if="item.orderCommoditys.length == 1 || l == 0" :rowspan="item.orderCommoditys.length">
                    <el-button type="text" v-has @click="showOrder(item.id)">订单详情</el-button>
                  </td>
                </tr>
              </tbody>
            </table>
          </div>
        </div>

        <div class="order_empty" v-if="list.length == 0">暂无数据</div>
      </div>
      <div class="table_footer" style="justify-content: flex-end;" v-if="total != 0">
        <el-pagination @current-change="changePage" class="pages" background hide-on-single-page layout="prev, pager, next" :total="total"></el-pagination>
      </div>
    </div>
  </el-container>
</template>

<script>
import DatePicker from '@/components/DatePicker/index';
import orderExpress from './components/orderExpress';
import orderAddress from './components/orderAddress';
import {getAfterList} from '@/api/shop/order';
import {objextend} from '@/utils/index.js';
export default {
  data() {
    return {
      type: [
        {
          value: 7,
          label: '订单编号'
        },
        {
          value: 1,
          label: '用户ID'
        },
        {
          value: 2,
          label: '用户信息'
        },
        {
          value: 3,
          label: '收件人信息'
        },
        {
          value: 4,
          label: '地址信息'
        },
        {
          value: 5,
          label: '商品名称'
        },
        {
          value: 6,
          label: '商品编号'
        }
      ],
      pay_type: [
        {
          value: '',
          label: '全部'
        },
        {
          value: 1,
          label: '余额'
        },
        {
          value: 2,
          label: '微信'
        },
        {
          value: 3,
          label: '支付宝'
        },
        {
          value: 4,
          label: '后台支付'
        }
      ],
      after_type: [
        {
          value: '',
          label: '全部'
        },
        {
          value: 1,
          label: '仅退款'
        },
        {
          value: 2,
          label: '退货退款'
        },
        {
          value: 3,
          label: '换货'
        }
      ],
      status: [
        {
          value: '',
          label: '全部'
        },
        {
          value: 1,
          label: '待支付'
        },
        {
          value: 2,
          label: '待发货'
        },
        {
          value: 13,
          label: '待核销'
        },
        {
          value: 3,
          label: '已发货'
        },
        {
          value: 4,
          label: '已完结'
        },
        {
          value: 5,
          label: '已评价'
        },
        // {
        //   value: 6,
        //   label: '已删除'
        // },
        {
          value: 7,
          label: '已收货'
        },
        {
          value: 11,
          label: '已关闭'
        }
      ],
      after_status: [
        {
          value: '',
          label: '全部'
        },
        {
          value: 1,
          label: '维权中'
        },
        {
          value: 2,
          label: '维权完成'
        },
        {
          value: 3,
          label: '未维权'
        }
      ],
      obj: {
        type: 7,
        keyword: '',
        pay_type: '',
        after_type: '',
        start_time: '',
        end_time: '',
        status: '',
        after_status: '',
        order_type: ''
      },
      page: 1,
      size: 10,
      objTemp: {},
      total: 0,
      isLoading: false,
      list: []
    };
  },
  components: {
    DatePicker
  },
  filters: {
    after_status_text: function(value) {
      switch (value) {
        case 1:
          return '仅退款';
          break;
        case 2:
          return '退货退款';
          break;
        case 3:
          return '换货';
          break;
        default:
          return '-';
          break;
      }
    }
  },
  created() {
    if (this.$route.query) {
      // if(this.$route.query.type){
      //   this.$route.query.type= Number(this.$route.query.type);
      // }
      for (let key in this.$route.query) {
        this.$route.query[key]=Number(this.$route.query[key])
      }


      this.obj = objextend(this.obj, this.$route.query);
    }
  },
  mounted() {
    this.searchList();
  },
  methods: {
    //复制单号
    doCopy(orderNo) {
      this.$copyText(orderNo).then(
        res => {
          //this.$alert('复制成功');
          this.$message({
            message: '复制成功',
            type: 'success',
            duration: 2000
          });
        },
        error => {
          this.$alert('无法复制');
        }
      );
    },
    //获取时间
    getTime(value) {
      this.obj.start_time = value[0];
      this.obj.end_time = value[1];
    },
    //查询数据
    searchList() {
      //复制查询对象
      this.objTemp = Object.assign({}, this.obj);
      this.getDataList();
    },
    //查询数据本体
    getDataList() {
      if (this.objTemp.status == 13) {
        this.objTemp.status = 2;
        this.objTemp.order_type = 2;
      }
      this.isLoading = true;
      getAfterList(this.objTemp, this.page, this.size)
        .then(res => {
          this.isLoading = false;
          let o = res.msg;
          //处理sku规格数据
          o.data.forEach(e => {
            if (e.orderCommoditys.length > 0) {
              e.orderItemCountPrice = 0;
              e.orderCommoditys.forEach(x => {
                if (x.pvs_value) {
                  //转换处理SKU数据->红+L+大
                  let l = x.pvs_value.split(','),
                    s = '';
                  l.forEach(k => {
                    s += k.split(':')[1] + '+';
                  });
                  s = s.substring(0, s.length - 1);
                  x.pvs_value_new = s;
                }
                 e.orderItemCountPrice += x.sell_price * x.count;
                console.log(e.attach && e.attach.length > 0 && x.attach_value && x.attach_value != '')
                if (e.attach && e.attach.length > 0 && x.attach_value && x.attach_value != '') {
                  x.attachlist = [];
                  x.attach_pirce = 0;

                  e.attach.forEach(a => {
                    if (x.attach_value.indexOf(a.attach_value) > -1 && x.attachlist.findIndex(_ => _.attach_value == a.attach_value) == -1) {
                   if(x.sku_id){
                       console.log(x.sku_id)
                     if(x.sku_id==a.sku_id){
                       x.attachlist.push(a);
                       x.attach_pirce += Number(a.attach_price) * a.num;
                     }
                   }else{
                     x.attachlist.push(a);
                     x.attach_pirce += Number(a.attach_price) * a.num;
                   }
                    }
                  });
                   e.orderItemCountPrice += x.attach_pirce;
                  x.attach_pirce = x.attach_pirce.toFixed(2);
                }
              });
            }
            e.orderItemCountPrice = e.orderItemCountPrice.toFixed(2);
          });

          this.list = o.data;
          this.total = o.total;
        })
        .catch(e => {
          this.isLoading = false;
        });
    },
    //查看维权详情
    showAfter(id) {
      this.$router.push({path: '/order/list/afterItem', query: {id: id, k: Math.random()}});
    },
    //查看订单详情
    showOrder(id) {
      this.$router.push({path: '/order/list/item', query: {id: id, k: Math.random()}});
    },
    //切换分页
    changePage(page) {
      this.page = page;
      this.getDataList();
    }
  }
};
</script>

<style lang="scss" scoped>
.proImg {
  display: flex;
  align-items: center;
}

//订单列表
.orderlist {
  width: 100%;
  display: flex;
  flex-direction: column;

  .order_head {
    border-bottom: 1px solid #dcdfe6;
    font-weight: 700;
    color: #909399;
    font-size: 14px;

    table {
      tr {
        td {
          padding: 10px 20px 20px;
          // text-align: center;
        }
      }
    }
  }

  .order_empty {
    color: #c0c4cc;
    font-size: 14px;
    text-align: center;
    height: 50px;
    line-height: 50px;
  }

  .order {
    margin-top: 15px;

    &:first-child {
      margin-top: 0;
    }

    .header {
      background-color: #f4f6f8;
      height: 40px;
      line-height: 40px;
      display: flex;
      border: 1px solid #dcdfe6;
      border-bottom: 0;
      padding-left: 20px;
      color: #909399;
      font-size: 14px;

      .txt {
        flex: 1;

        .info {
          display: inline-block;
          margin-right: 15px;
        }
      }

      .button {
        width: 300px;
        text-align: right;
        padding-right: 20px;
      }
    }

    .content {
      background: #dcdfe6;
      font-size: 14px;
      color: #606266;

      table {
        background: #fff;

        tr {
          td {
            padding: 15px;
            line-height: 28px;
            //border: 1px solid #DCDFE6;

            .pro {
              display: flex;

              .img {
                width: 80px;
                margin-right: 10px;
              }

              .txt {
                flex: 1;
                display: flex;
                flex-direction: column;
                align-content: space-between;

                .name {
                  .t0 {
                    color: #409eff;
                    font-weight: 700;
                    margin-right: 5px;
                  }

                  .t1 {
                    color: #f56c6c;
                    font-weight: 700;
                    margin-right: 5px;
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
.pricecount{
  div{
    padding: 5px 0;
    span:first-child{
      display: inline-block;
      width: 75px;
       text-align: right;
       margin-right: 5px;
    }
  }
}
</style>
